package edu.gatech.oad.mule;

import java.util.Random;

import javax.swing.JOptionPane;

/**
 * This class represents a Random Event in the game.  A random event can 
 * either add or subtract resources from a Player.  A negative event can only 
 * occur to the highest scoring Players, and positive events can only occur to 
 * the lowest scoring Players.
 *
 * @author Brendan McGarry, Dale Avery, D'Miria Collins, Natasha Parekh, Jaemin Park
 * @version %I%, %G%
 */
public class RandomEvent{
	
	//constraints for the event (if it has to be a 
	//  good event, bad event, or no constraint at all)
	public static enum eventConstraint{
		GOOD, BAD, NONE
	};
	
	//for calculating if a random event occurs
	private static Random r = new Random();
	
	//round bonuses
	private static int[] bonus = {25, 25, 25, 50, 50, 50, 50, 75, 75, 75, 75, 100};
	
	/**
	 * This method distributes out a random event to the inputted
	 * player.  This method ensures the player in first cannot get
	 * bonuses and the player in last cannot get penalized
	 * 
	 * @param p The player to give this random event
	 * @param round The round of the game
	 * @param ec An event constraint to modify how the random events are distributed
	 * out to players.  This ensures that the players in last cannot get penalized
	 * and the players in first cannot get bonuses
	 */
	public static void distribute(Player p, int round, eventConstraint ec){
		System.out.print(p.getName() + " is rolling for a random event... ");
		//generate a random number from [0, 100).  If the result is
		//  [0, 26], then this player gets a random event
		if(r.nextInt(100) < 27){
			//logic for what random event the player gets
			int roll = 0;
			//good events are [0, 3]
			if(ec == eventConstraint.GOOD){
				roll = r.nextInt(4);
			}//end if
			//bad events are [4, 6]
			else if(ec == eventConstraint.BAD){
				roll = r.nextInt(3) + 4;
			}//end else if
			//don't care? then go for any
			else{
				roll = r.nextInt(7);
			}//end else
			//switch statement for the random events
			switch(roll){
				case 0:
					System.out.println(p.getName().toUpperCase() + " JUST RECEIVED A PACKAGE FROM THE GT ALUMNI CONTAINING 3 FOOD AND 2 ENERGY UNITS.");
					p.addFood(3);
					p.addEnergy(2);
					break;
				case 1:
					System.out.println("A WANDERING TECH STUDENT REPAID " + p.getName().toUpperCase() + "'S HOSPITALITY BY LEAVING TWO BARS OF ORE.");
					p.addSmithore(2);
					break;
				case 2:
					System.out.println("THE MUSEUM BOUGHT " + p.getName().toUpperCase() + "'S ANTIQUE PERSONAL COMPUTER FOR $" + (bonus(8, round)) + ".");
					p.addMoney(bonus(8, round));
					break;
				case 3:
					System.out.println(p.getName().toUpperCase() + " FOUND A DEAD MOOSE RAT AND SOLD THE HIDE FOR $" + (bonus(2, round)) + ".");
					p.addMoney(bonus(2, round));
					break;
				case 4:
					System.out.println("FLYING CAT-BUGS ATE THE ROOF OFF " + p.getName().toUpperCase() + "'S HOUSE. REPAIRS COST $" + (bonus(4, round)) + ".");
					p.addMoney(bonus(-4, round));
					break;
				case 5:
					System.out.println("MISCHIEVOUS UGA STUDENTS BROKE INTO " + p.getName().toUpperCase() + "'S STORAGE SHED AND STOLE HALF YOUR FOOD.");
					p.addMoney(-1 * (p.getMoney() / 2));
					break;
				case 6:
					System.out.println(p.getName().toUpperCase() + "'S SPACE GYPSY INLAWS MADE A MESS OF THE TOWN. IT COST YOU $" + (bonus(6, round)) + " TO CLEAN IT UP.");
					p.addMoney(bonus(-6, round));
					break;
			}//end switch
		}//end if
		else{
			System.out.println("But did not recieve anything.");
		}//end else
	}//end method distribute
	
	/**
	 * Calculates the bonus to be awarded to the player.  This is based off of the 
	 * round and a special multiplier
	 * 
	 * @param multiplier How much to multiply the round bonus by
	 * @param round The current round; this is used to calculate the round bonus
	 * @return The total amount of bonus earnings for the player
	 */
	private static int bonus(int multiplier, int round){
		//keep round in bounds to prevent a null pointer exception
		//  when accessing the round bonus array
		if(round < 0){
			round = 0;
		}//end if
		else if(round >= bonus.length){
			round = bonus.length - 1;
		}//end else if
		return (multiplier * bonus[round]);
	}//end method bonus
	
}//end class RandomEvent